<?php
/**
 * 试卷模块
 * 
 * @author river_he@eueang.com
 */
final class Gos_Model_Exam extends Gospel_Model_Abstract {
	/**
	 * 未审核
	 * 
	 * @var int
	 */
	const ST_UNCHECK = 1;
	
	/**
	 * 已审核
	 * 
	 * @var int
	 */
	const ST_CHECKED = 2;
	
	protected function init(){	
		$this->mainDao = new Gos_Dao_Exam();
	}
	
	/**
	 * 获取单条记录
	 * 
	 * @param mixed $where
	 * @param boolean $isDetail
	 * @return array
	 */
	public function getItem($where, $isDetail=false){
		$return = array();
		if(!empty($where)){
			$where = $this->getWhere($where);
			$return = $this->mainDao->getOne($where);
			if($return){
				$examAnswer = new Gos_Dao_Exam_Answer();
				$answer = $examAnswer->getOne('nEid='.$return['nId']);
				if($answer){
					array_shift($answer);
					$return = array_merge($return, $answer);
				}
				if($isDetail){
					$examQuest = new Gos_Dao_Exam_Question_View();
					$questions = $examQuest->getList('nEid='.$return['nId'], 'nType ASC', 0, 100);
					$questArr = array();
					if(is_array($questions)){
						$questArr['choice'] = array();
						$questArr['fill'] = array();
						$questArr['opinion'] = array();
						$questArr['essay'] = array();
						$questArr['listen'] = array();
						$questionItem = new Gos_Dao_Question_Item();
						foreach($questions as $quest){
							switch($quest['nType']){
								case Gos_Model_Question::QT_CHOICE:
									$quest['items'] = $questionItem->getList('nQid='.$quest['nQid'], 'sItemName ASC', 0, 10);
									array_push($questArr['choice'], $quest);
									break;
								case Gos_Model_Question::QT_FILL:
									array_push($questArr['fill'], $quest);
									break;
								case Gos_Model_Question::QT_OPINION:
									array_push($questArr['opinion'], $quest);
									break;
								case Gos_Model_Question::QT_LISTEN:
									array_push($questArr['listen'], $quest);
									break;
								case Gos_Model_Question::QT_ESSAY:
									array_push($questArr['essay'], $quest);
									break;
							}
						}
					}
					(!empty($questArr['choice']))?usort($questArr['choice'], 'Gos_Model_Exam::questSort'):'';
					(!empty($questArr['fill']))?usort($questArr['fill'], 'Gos_Model_Exam::questSort'):'';
					(!empty($questArr['opinion']))?usort($questArr['opinion'], 'Gos_Model_Exam::questSort'):'';
					(!empty($questArr['listen']))?usort($questArr['listen'], 'Gos_Model_Exam::questSort'):'';
					(!empty($questArr['essay']))?usort($questArr['essay'], 'Gos_Model_Exam::questSort'):'';
					$return['questions'] = $questArr;
				}
			}
		}
		return $return;
	}
	
	/**
	 * 获取多条记录
	 * 
	 * @param mixed $where
	 * @param string $sort
	 * @param int $page
	 * @param int $psize
	 * @return array
	 */
	public function getItems($where, $sort, $offset=0, $psize=10){
		$return = array();
		if(!empty($where)){
			$where = $this->getWhere($where);
			$items = $this->mainDao->getList($where, $sort, $offset, $psize);
			$page = $this->getPage($this->mainDao,$where, $offset, $psize);
			$return['data'] = $items;
			$return['pager'] = $page;
			unset($items);
			unset($page);
		}
		return $return;
	}
	
	/**
	 * 获取统计数量
	 * 
	 * @param mixed $where
	 * @return int
	 */
	public function getTotal($where){
		
	}
	
	/**
	 * 添加记录
	 * 
	 * @param array $item、
	 * @return int
	 */
	public function append(array $item){
		
	}
	
	/**
	 * 修正记录
	 * 
	 * @param mixed $where
	 * @param array $set
	 * @return int
	 */
	public function alter($where, array $set){
		
	}
	
	private static function questSort(array $qa, array $qb){
		$return = 0;
		if($qa['nSeq'] > $qb['nSeq']){
			$return = 1;
		}elseif($qa['nSeq'] < $qb['nSeq']){
			$return = -1;
		}
		return $return;
	}
}
//end